package com.gugu.procedure.controller;


import com.gugu.procedure.bean.WorkDone;
import com.gugu.procedure.common.MessageResult;
import com.gugu.procedure.service.ProductionRecordService;
import com.gugu.procedure.service.StaffService;
import com.gugu.procedure.service.WorkDoneService;
import com.gugu.procedure.util.OrderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

@RestController
@Api(tags = {"用工模块"})
@RequestMapping("workDone")
public class WorkDoneController extends BaseController {


    @Autowired
    private WorkDoneService workDoneService;
    @Autowired
    private ProductionRecordService productionRecordService;
    @Autowired
    private StaffService staffService;


    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public MessageResult page(String orderNo, String account, int pageNo, int pageSize) {
        return workDoneService.page(orderNo, account, pageNo, pageSize);
    }

    @GetMapping("delete")
    @ApiOperation(value = "删除")
    public MessageResult delete(Long id) {
        workDoneService.delete(id);
        return MessageResult.success();
    }

    @GetMapping("add")
    @ApiOperation(value = "导入")
    public MessageResult save(String path) {
        String account = super.getAccount();
        File file = new File(pathPrefix + path);
        if (!file.exists()) {
            return MessageResult.error("文件不存在");
        }
        List <WorkDone> list = OrderUtil.resolvingWorkDone(file, account);
        // 增加工序记录
        for (int i = 0; i < list.size(); i++) {
            WorkDone workDone = list.get(i);
            workDoneService.save(workDone);
        }
        return MessageResult.success();
    }

    @GetMapping("listStaffProductionRecord")
    @ApiOperation(value = "用工列表")
    public MessageResult listStaffProductionRecord(String orderNo, String staffNumber, String startTime, String endTime, String production, String status, int pageNo, int pageSize) {
        return productionRecordService.
                listStaffProductionRecord(orderNo, staffNumber, startTime, endTime, production, status, pageNo, pageSize);
    }


    @GetMapping("listProduction")
    @ApiOperation(value = "用工列表的工序下拉")
    public MessageResult <List <String>> listProduction() {
        return productionRecordService.listProduction();
    }

}
